import threading
from plugin.utils.redis import redis


class Thread(threading.Thread):

    def __init__(self, function, threadID , args=None):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.function = function
        self.args = args
        redis.set(threadID, {'status':'init'})

    def run(self):
        """
        开始运行
        :return:
        """
        redis.set(self.threadID,  {'status':'start'})
        try:
            # 执行需要运行的代码
            self.function(self.args)
            redis.set(self.threadID, {'status': 'end'}, 60)
        except Exception as e:
            # 捕获错误 放入redis
            redis.set(self.threadID, {'status': 'end', 'message': str(e)}, 60)




